from time import time

from django.conf import settings
from django.db import models


class StudentInfo(models.Model):
    """
        学生请假信息
    """
    name = models.CharField(max_length=32, verbose_name='学生姓名', null=False, blank=False)
    username = models.CharField(max_length=32, verbose_name='学生号或用户名', unique=True)
    password = models.CharField(max_length=32, verbose_name='离校系统登录密码')
    area = models.IntegerField(verbose_name='地区', choices=(
        (0, '临港'),
        (1, '闵行'),
    ), default=0, null=False, blank=False)
    mobile = models.CharField(max_length=32, verbose_name='联系电话', default='15904090058', null=False, blank=False)
    mobile_spe = models.CharField(max_length=32, verbose_name='紧急联系电话', default='15804093389', null=False, blank=False)
    target = models.CharField(max_length=32, verbose_name='离校目的地', default='滴水湖', null=False, blank=False)
    reason = models.IntegerField(verbose_name='回家原因', choices=(
        (0, '回家'),
        (1, '实习'),
        (2, '其他'),
    ), default=0)
    next = models.IntegerField(verbose_name='下一步', choices=(
        (0, '办结'),
        (1, '送辅导员审核'),
    ), default=0)
    add_time = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    class Meta:
        verbose_name = '学生信息'
        verbose_name_plural = verbose_name

    def image_record_path(self):
        return f'{settings.SCREEN_SHOOTS_ROOT}/{self}-record-{time()}.png'

    def image_tip_path(self):
        return f'{settings.SCREEN_SHOOTS_ROOT}/{self}-tip-{time()}.png'

    def __str__(self):
        return f'{self.name or self.username}'


class StudentGroup(models.Model):
    """
        学生分组
    """
    name = models.CharField(verbose_name='分组名称', max_length=32, null=False, blank=False)
    is_default = models.BooleanField(verbose_name='是否是默认分组', default=False)
    students = models.ManyToManyField(verbose_name='学生', to=StudentInfo)
    add_time = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    class Meta:
        verbose_name = '学生分组'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{" [默认分组] " if self.is_default else ""}{self.name}({self.students.count()}人)'


class ForLeaveRecord(models.Model):
    """
        请假记录
    """
    image_tip = models.ImageField(verbose_name='操作完成截图', upload_to='screenshoots/', default='static/failed.png')
    image_record = models.ImageField(verbose_name='查看结果截图', upload_to='screenshoots/', default='static/failed.png')
    student = models.ForeignKey(to=StudentInfo, on_delete=models.DO_NOTHING)
    add_time = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    class Meta:
        verbose_name = '请假记录'
        verbose_name_plural = verbose_name
        ordering = ['-add_time']

    def __str__(self):
        return f'{self.student.name} - {self.add_time}'
